import React, { useState } from 'react';
import { Form, Button, Input, Modal } from 'antd';

import { UserRoleSetParam } from '../data.d';

export interface RoleFormProps {
  onCancel: (flag?: boolean, formVals?: UserRoleSetParam) => void;
  onSubmit: (values: UserRoleSetParam) => void;
  roleModalVisible: boolean;
  values: UserRoleSetParam;
}
const FormItem = Form.Item;

const formLayout = {
  labelCol: { span: 7 },
  wrapperCol: { span: 13 },
};

const RoleForm: React.FC<RoleFormProps> = (props) => {
  const [formValues, setFormValues] = useState<UserRoleSetParam>({
    id: props.values.id,
    loginName: props.values.loginName,
    oldRole: props.values.oldRole,
    newRole: props.values.newRole,
  });

  const [form] = Form.useForm();

  const {
    onSubmit: handleDelete,
    onCancel: handleRoleModalVisible,
    roleModalVisible,
  } = props;

  const handleRealModify = async () => {
    const fieldsValue = await form.validateFields();
    setFormValues({ ...formValues, ...fieldsValue });
    handleDelete({ ...formValues, ...fieldsValue });
  }

  const renderFooter = () => {
    return (
      <>
        <Button onClick={() => handleRoleModalVisible(false)}>取消</Button>
        <Button type="primary" onClick={() => handleRealModify()}>修改</Button>
      </>
    );
  };

  return (
    <Modal
      width={640}
      bodyStyle={{ padding: '32px 40px 48px' }}
      destroyOnClose
      title="修改用户权限"
      visible={roleModalVisible}
      footer={renderFooter()}
      onCancel={() => handleRoleModalVisible(false)}
    >
      <Form
        {...formLayout}
        form={form}
        initialValues={{
          id: formValues.id,
          loginName: formValues.loginName,
          oldRole: formValues.oldRole,
          newRole: formValues.newRole,
        }}
      >
        <FormItem
          name="id"
          label="id"
        >
          <Input disabled />
        </FormItem>
        <FormItem
          name="loginName"
          label="登陆用户"
        >
          <Input disabled />
        </FormItem>
        <FormItem
          name="oldRole"
          label="老权限"
        >
          <Input disabled />
        </FormItem>
        <FormItem
          name="newRole"
          label="新权限"
        >
          <Input />
        </FormItem>
      </Form>
    </Modal>
  );
};

export default RoleForm;
